package com.voxel.sense.infomsg.module.login;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.text.InputType;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.bigkoo.pickerview.builder.OptionsPickerBuilder;
import com.bigkoo.pickerview.listener.CustomListener;
import com.bigkoo.pickerview.listener.OnOptionsSelectListener;
import com.bigkoo.pickerview.view.OptionsPickerView;
import com.example.com.common.app.BViewActivity;
import com.example.com.common.utils.UIUtil;
import com.example.com.common.view.dialog.IDialog;
import com.example.com.common.view.dialog.InputDialogCallBack;
import com.voxel.sense.infomsg.MainActivity;
import com.voxel.sense.infomsg.R;
import com.voxel.sense.infomsg.module.user.regist.RegisterActivity;

import butterknife.BindView;
import butterknife.OnClick;
import butterknife.OnFocusChange;
import cyy.example.com.facotry.share.SPEngine;

/**
 * 登录界面
 */
public class LoginActivity extends BViewActivity<LoginContract.Presenter>
        implements LoginContract.View {

    @BindView(R.id.bt_login)
    Button bt_login;//登陆按钮
    @BindView(R.id.et_username)
    EditText et_username;
    @BindView(R.id.et_password)
    EditText et_password;//密码输入框 compatibility
    @BindView(R.id.cl_group)
    View cl_group;
    @BindView(R.id.iv_eyes)
    ImageView iv_eyes;
    @BindView(R.id.bt_regist)
    Button bt_regist;

    //是否明文显示密码标记
    boolean mIsShowPsw = false;

    @BindView(R.id.tv_select_service)
    TextView tv_select_service;
    private OptionsPickerView mPvCustomOptions;//条件选择器

    String userNameValue, passwordValue;
    private Dialog mInpudialog;
    //    private List<BaseUrlEntity.BodyBean.BaseUrlBean> mUrlEntity;

    public static void show(Context context) {
        context.startActivity(new Intent(context, LoginActivity.class));
    }

    @Override
    protected int getContentLayoutId() {
        return R.layout.activity_login;
    }

    @Override
    protected LoginContract.Presenter initPresenter() {
        return new LoginPresenter(this);
    }


    @Override
    protected void initWidget() {
        super.initWidget();
    }

    @Override
    protected void initData() {
        super.initData();
        setPswTextVisible();
        //设置状态栏颜色
        UIUtil.setStatusBarColor(this, R.color.main_background);
    }

    @Override
    public void initPswEditText(boolean flag, String psw) {
        et_username.setText(psw);
    }

    @Override
    public void setAccount(String str) {
        et_username.setText(str);
        et_username.setSelection(str.length());
    }

    @Override
    public void onShowIdCardDialog() {
        if (mInpudialog == null) {
            mInpudialog = IDialog.getInstance().showInputDialog(this, "身份证验证", "请输入身份证号码",
                    "取消", "上传", false, false, new InputDialogCallBack() {
                        @Override
                        public void onClick(int what, String inputText) {
                            if (what == IDialog.YES) {
                                mPresenter.upIdCardNumb(inputText);
                            } else {
                                mInpudialog.dismiss();
                            }
                        }
                    });
        } else {
            mInpudialog.show();
        }
    }

    @Override
    public void onDismissIdcardDialog() {
        if (mInpudialog != null && mInpudialog.isShowing()) {
            mInpudialog.dismiss();
        }
    }

    @Override
    protected void initEven() {
        //记住密码选项
        testPickUrl();
    }




    //登录点击事件
    @OnClick({R.id.bt_login, R.id.iv_eyes,R.id.bt_regist})
    void onClickLogin(View view) {
        int id = view.getId();
        if (id == R.id.bt_login) {//登陆
            startLogin();
//            loginSuccess();
        } else if (id == R.id.iv_eyes) {//密码显示状态
            //改变状态
            mIsShowPsw = !mIsShowPsw;
            setPswTextVisible();
        } else if (id == R.id.bt_regist) {
            RegisterActivity.show(this);
        }
    }

    @OnFocusChange(R.id.et_password)
    void onFocusChangeLisener(View v, boolean hasFocus){
        if (hasFocus) {
            et_password.setSelection(et_password.length());
        }
    }

    /**
     * 设置密码是否可见
     */
    private void setPswTextVisible() {

        if (mIsShowPsw) {
            et_password.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
            iv_eyes.setImageResource(R.drawable.ic_show_psw);
        } else {
            et_password.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
            iv_eyes.setImageResource(R.drawable.ic_icon_hint_psw);
        }
        et_password.setSelection(et_password.length());
    }

    /**
     * 开始登录操作
     */
    public void startLogin() {
        userNameValue = et_username.getText().toString();
        passwordValue = et_password.getText().toString();
        mPresenter.login(userNameValue.trim(), passwordValue.trim());
    }

    @Override
    protected void onPause() {
        super.onPause();
        dismissProgressDialog();
    }

    @Override
    public void loginSuccess() {
        MainActivity.show(this);
        finish();
    }

    @Override
    public void loginFailure(String msg) {

    }

    private void testPickUrl() {
        String currentEvnName = SPEngine.getSPEngine().getCurrentEvnName();

        tv_select_service.setText(currentEvnName);

        tv_select_service.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mPvCustomOptions.show();
            }
        });

        mPvCustomOptions = new OptionsPickerBuilder(this, new OnOptionsSelectListener() {
            @Override
            public void onOptionsSelect(int options1, int options2, int options3, View v) {
//                BaseUrlEntity.BodyBean.BaseUrlBean bean = mUrlEntity.get(options1);
//                tv_select_service.setText(bean.getDistrict());
//                SPEngine.getSPEngine().setCurrentEvnName(bean.getDistrict());
//                ControlEvnUtils.alterUrl(bean.getAPI_URL(), bean.getFileUrl(), bean.getFileUrlWeb());

            }
        })
                .setLayoutRes(R.layout.pickerview_custom_options, new CustomListener() {//初始化布局
                    @Override
                    public void customLayout(View v) {
                        final TextView tvSubmit = v.findViewById(R.id.tv_finish);
                        ImageView ivCancel = v.findViewById(R.id.iv_cancel);
                        //点击完成
                        tvSubmit.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                mPvCustomOptions.returnData();
                                mPvCustomOptions.dismiss();
                            }
                        });
                        //点击取消
                        ivCancel.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                mPvCustomOptions.dismiss();
                            }
                        });
                    }
                })
//                .isDialog(true)
                .build();
//        mUrlEntity = ControlEvnUtils.getURlEntity();
//        List<String> list = new ArrayList();
//
//        for (BaseUrlEntity.BodyBean.BaseUrlBean bean : mUrlEntity) {
//            list.add(bean.getDistrict());
//        }
//
//        mPvCustomOptions.setPicker(list);
    }
}
